package com.zjhome.cloudnote.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.zjhome.cloudnote.domain.DirInfo;
import com.zjhome.cloudnote.domain.NoteInfo;
import com.zjhome.cloudnote.domain.RecycleInfo;
import com.zjhome.cloudnote.mapper.RecycleMapper;
import com.zjhome.cloudnote.service.DirService;
import com.zjhome.cloudnote.service.NoteService;
import com.zjhome.cloudnote.service.RecycleService;
import com.zjhome.cloudnote.util.ExplorerUtil;

@Service
public class RecycleServiceImpl implements RecycleService {
	@Autowired
	private RecycleMapper	recycleMapper;
	
	@Autowired
	private DirService		dirService;
	@Autowired
	private NoteService 	noteService;

	@Override
	public RecycleInfo selectRecycleById(String id, String uid) throws Exception {
		// 
		return recycleMapper.selectRecycleById(id, uid);
	}

	@Override
	public List<RecycleInfo> selectRecycleByPara(RecycleInfo recycleInfo) throws Exception {
		// 
		return recycleMapper.selectRecycleByPara(recycleInfo);
	}

	@Override
	public int insertRecycleByParam(RecycleInfo recycleInfo) throws Exception {
		// 生成id		
		if (StringUtils.isEmpty(recycleInfo.getId())) {
			recycleInfo.setId(UUID.randomUUID().toString());
		}		
		return recycleMapper.insertRecycleByParam(recycleInfo);
	}

	@Override
	public int updateRecycleByParam(RecycleInfo recycleInfo) throws Exception {
		// 
		return recycleMapper.updateRecycleByParam(recycleInfo);
	}

	@Override
	public int deleteRecycleById(String id, String uid) throws Exception {
		// 
		return recycleMapper.deleteRecycleById(id, uid);
	}
	
	@Override
	public int restoreRes(String[] ids, String uid) throws Exception {
		// 从回收站 恢复 资源
		// 遍历所有的条目id
		for (int i=0; i<ids.length; i++) {
			RecycleInfo recycleInfo = this.selectRecycleById(ids[i], uid);
			if (ExplorerUtil.judgeResType(recycleInfo.getRestype()) == ExplorerUtil.RESTYPE_DIR) {
				noteService.updateNoteDelFlagByDir(recycleInfo.getResid(), uid, 0);	// 更新笔记状态
				// 文件夹
				dirService.updateDirDelFlagByDir(recycleInfo.getResid(), uid, 0);	// 更新子文件夹状态
				
				// 恢复本文件夹状态
				DirInfo dirInfo = new DirInfo();
				
				dirInfo.setId(recycleInfo.getResid());
				dirInfo.setUid(uid);
				dirInfo.setDelflag(0);
				dirService.updateDirByParam(dirInfo);
			}
			else {
				// 笔记
				NoteInfo noteInfo = new NoteInfo();
				
				noteInfo.setId(recycleInfo.getResid());
				noteInfo.setUid(uid);
				noteInfo.setDelflag(0);

				noteService.updateNoteByParam(noteInfo);
			}
			
			// 删除记录
			recycleMapper.deleteRecycleById(ids[i], uid);
		}
		
		return 0;
	}

	@Override
	public int deleteRes(String[] ids, String uid) throws Exception {
		// 彻底删除
		// 遍历所有的id
		for (int i=0; i<ids.length; i++) {			
			RecycleInfo recycleInfo = this.selectRecycleById(ids[i], uid);
			if (ExplorerUtil.judgeResType(recycleInfo.getRestype()) == ExplorerUtil.RESTYPE_DIR) {
				// 文件夹
				// 需要先删除 子文件下的笔记！！！ 否则文件夹结构被删除后，会导致一部分 子笔记 无法删除，编程僵尸笔记 
				noteService.deleteSubNoteById(recycleInfo.getResid(), uid);	// 删除所有笔记				
				dirService.deleteSubDirById(recycleInfo.getResid(), uid);	// 删除所有子文件夹				
				dirService.deleteDirById(recycleInfo.getResid(), uid);	// 删除父文件夹
			}
			else {
				// 笔记
				noteService.deleteNoteById(recycleInfo.getResid(), uid);
			}
			// 删除记录
			recycleMapper.deleteRecycleById(ids[i], uid);
		}
		
		return 0;
	}

	@Override
	public int truncateRecycle(String uid) throws Exception {
		// 清空回收站
		RecycleInfo recycleInfo = new RecycleInfo();
		recycleInfo.setUid(uid);
		
		// 获取 该用户 所有回收站条目
		List<String> ids = new ArrayList<String>();
		List<RecycleInfo> recList = this.selectRecycleByPara(recycleInfo);
		recList.forEach(item -> ids.add(item.getId()));
				
		return this.deleteRes(ids.toArray(new String[recList.size()]), uid);
	}
}
